﻿using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace Chimp.Core
{
    public class UIFrame : ChimpWidget
    {
        public UIButton CloseBut;
        public ChimpTexture2D Distort;
        public UIButton HideBut;
        public bool IsActive;
        public bool MouseOver;
        public bool MousePush;
        public UIButton ResizeBut;
        public UIButton TitleBar;

        public UIFrame(float x, float y, float w, float h, ChimpTexture2D img, ChimpWidget p)
            : base(new Vector2(x, y), new Vector2(w, h), p)
        {
            Image = img;
            DistortBG = false;
            NoRender = false;
        }

        public UIFrame(float x, float y, float w, float h, string title)
            : base(new Vector2(x, y), new Vector2(w, h), null)
        {
            Text = title;
            Image = new ChimpTexture2D("Res/UI/guiframe1.png");
            Distort = new ChimpTexture2D("Res/UI/framenorm1.jpg");
            if (title != "-1")
            {
                Rebuild(w, h, title, true);
            }
            Pos += (Size/2.0f);
            DistortBG = true;
        }

        public UIFrame(float x, float y, float w, float h, string title, ChimpWidget p)
            : base(new Vector2(x, y), new Vector2(w, h), p)
        {
            Text = title;
            Image = new ChimpTexture2D("Res/UI/guiframe1.png");
            Distort = new ChimpTexture2D("Res/UI/framenorm1.jpg");
            if (title != "-1")
            {
                Rebuild(w, h, title, false);
            }
            DistortBG = true;
        }

        public ChimpTexture2D Image { get; set; }
        public bool DistortBG { get; set; }

        private void Rebuild(float w, float h, string title, bool scene)
        {
            if (Widgets.Contains(TitleBar)) Widgets.Remove(TitleBar);
            if (Widgets.Contains(HideBut)) Widgets.Remove(HideBut);
            if (Widgets.Contains(CloseBut)) Widgets.Remove(CloseBut);
            if (Widgets.Contains(ResizeBut)) Widgets.Remove(ResizeBut);
            if (scene)
            {
                TitleBar = new UIButton(2, 2, w - 46, 15, title, this).IsSceneButton();
                HideBut = new UIButton(w - 44, 2, 20, 15, "v", this).IsSceneButton();
                CloseBut = new UIButton(w - 22, 2, 20, 15, "x", this).IsSceneButton();
                ResizeBut = new UIButton(w - 18, h - 18, 18, 15, "X", this).IsSceneButton();
            }
            else
            {
                TitleBar = new UIButton(2, 2, w - 46, 15, title, this);
                HideBut = new UIButton(w - 44, 2, 20, 15, "v", this);
                CloseBut = new UIButton(w - 22, 2, 20, 15, "x", this);
                ResizeBut = new UIButton(w - 18, h - 18, 18, 15, "X", this);
            }
        }

        public override void OnEnter()
        {
            MouseOver = true;
        }

        public override void OnExit()
        {
            MouseOver = false;
        }

        private float px, py;
        private bool fp = false;
        public override void OnMouseMove(float x, float y, float xi, float yi)
        {
            ChimpUI.Handler.OnMouseMove(this, x, y, xi, yi);
            if (MousePush)
            {
                if(fp==false)
                {
                    px = ChimpUI.MX;
                    py = ChimpUI.MY;
                    fp = true;
                }
                NotifyParent("Drag", "Move", this, (object)(ChimpUI.MX-px), (object) (ChimpUI.MY-py));
                px = ChimpUI.MX;
                py = ChimpUI.MY;
            }
        }

        public override string OnNotify(string id, string msg, ChimpWidget w, params object[] dat)
        {
            switch (id)
            {
                case "Drag":
                    if (w == TitleBar)
                    {
                        Pos = Pos + new Vector2((float) dat[0], (float) dat[1]);
                    }
                    if (w == ResizeBut)
                    {
                        Size = Size + new Vector2((float) dat[0], (float) dat[1]);
                        //	Rebuild(Size.X,Size.Y,Text);
                        TitleBar.Size = new Vector2(TitleBar.Size.X + (float) dat[0], TitleBar.Size.Y);
                        CloseBut.Pos = new Vector2(CloseBut.Pos.X + (float) dat[0], CloseBut.Pos.Y);
                        HideBut.Pos = new Vector2(HideBut.Pos.X + (float) dat[0], HideBut.Pos.Y);
                        ResizeBut.Pos = ResizeBut.Pos + new Vector2((float) dat[0], (float) dat[1]);
                    }
                    break;
            }
            return "";
        }

        public override void OnMouseDown(int b)
        {
            MousePush = true;
            ChimpUI.Handler.OnMouseDown(this, b);
        }

        public override void OnMouseUp(int b)
        {
            MousePush = false;

            ChimpUI.Handler.OnClick(this, b);
            ChimpUI.Handler.OnMouseUp(this, b);
            fp = false;
        }

        public override void OnSceneRender()
        {
            ChimpDraw.BlendMode = ChimpBlend.Alpha;
            Alpha = 1.0f;
            Color = new Color4(1.0f, 1, 1, 1);
            if (DistortBG == false)
            {
               
                ChimpDraw.ImageDistort(new Vector4(DrawPos.X - Size.X/2, DrawPos.Y - Size.Y/2, Size.X, Size.Y),
                                       DrawRotation, DrawScale, new Vector4(Color.R, Color.G, Color.B, Alpha),
                                       ChimpScene.FBTex, Image, Distort, 0.02f*Alpha);
            }
            else
            {
                ChimpDraw.Image(new Vector4(DrawPos.X - Size.X/2, DrawPos.Y - Size.Y/2, Size.X, Size.Y), DrawRotation,
                                DrawScale, new Vector4(Color.R, Color.G, Color.B, Alpha), Image, true);
            }
        }

        public override void OnRender()
        {
            //BeginDraw();

            ChimpDraw.BlendMode = ChimpBlend.Alpha;
            if (DistortBG)
            {
                DrawImageDistort(ChimpScene.FBTex, Image, Distort, GlobalPos, Size,
                                 new Color4(Color.R, Color.G, Color.B, Alpha), 0.02f*Alpha);
            }
            else
            {
                //Console.WriteLine("ImgW:" + Image.Width + " ImgH:" + Image.Height);
                BeginDraw();
                DrawImage(Image, GlobalPos, Size);
            }
        }

        public void DrawImage(ChimpTexture2D Image, Vector2 gp, Vector2 Size)
        {
            DrawImage(Image, gp, Size, new Color4(1, 1, 1, 1));
        }

        public void DrawImageDistort(ChimpTexture2D src, ChimpTexture2D Image, ChimpTexture2D distort, Vector2 gp,
                                     Vector2 Size, Color4 col, float df)
        {
            ChimpDraw.ImageDistort(new Vector4(gp.X, gp.Y, Size.X, Size.Y), 0.0f, 1.0f,
                                   new Vector4(col.R, col.G, col.B, col.A), src, Image, distort, df);
        }

        public void DrawImage(ChimpTexture2D Image, Vector2 gp, Vector2 Size, Color4 col)
        {
            if (Image != null)
            {
                Image.Bind(0);
            }
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            GL.Begin(BeginMode.Quads);

            GL.TexCoord2(0, 0);
            GL.Vertex2(gp);

            GL.TexCoord2(1, 0);
            GL.Vertex2(new Vector2(gp.X + Size.X, gp.Y));

            GL.TexCoord2(1, 1);
            GL.Vertex2(gp + Size);

            GL.TexCoord2(0, 1);
            GL.Vertex2(new Vector2(gp.X, gp.Y + Size.Y));

            GL.End();

            if (Image != null)
            {
                Image.Unbind(0);
            }
            //   return gp;
        }

        public void BeginDraw()
        {
            GL.Color4(Color.R, Color.G, Color.B, GlobalAlpha);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            GL.Disable(EnableCap.AlphaTest);
            GL.Disable(EnableCap.DepthTest);
        }
    }
}